<template>
  <div class="page-member-manage-order mod-stock=flow">
    <div class="search-bar">
      <!-- native modifier has been removed, please confirm whether the function has been affected  -->
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        label-width="auto"

        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="stockBillNo"
            :label="$t('sys.nickName')+':'"
          >
            <el-input
              v-model="searchForm.nickName"
              clearable
              type="text"
              :placeholder="$t('sys.nickName')"
            />
          </el-form-item>
          <el-form-item :label="$t('order.buyTime')+':'">
            <el-date-picker
              v-model="createDateRange"

              type="datetimerange"
              clearable
              :range-separator="$t('date.tip')"
              value-format="YYYY-MM-DD HH:mm:ss"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0),
                              new Date(2000, 2, 1, 23, 59, 59)]"
              :start-placeholder="$t('text.startTime')"
              :end-placeholder="$t('date.end')"
              @change="createTimeChange"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="resetForm('searchForm')"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con">
        <el-table
          ref="dataListRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <el-table-column
            :label="$t('sys.nickName')"
            prop="nickName"
            align="left"
            width="190"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.nickName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="pic"
            :label="$t('publics.profilePicture')"
          >
            <template #default="scope">
              <div class="table-cell-image">
                <img
                  v-if="!scope.row.pic"
                  src="~@/assets/img/userImg.jpg"
                  width="130px"
                >
                <img
                  v-else
                  :src="checkFileUrl(scope.row.pic)"
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('order.buyLevel')"
            prop="levelName"
            align="left"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.levelName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('user.memberTime')"
            prop="term"
            align="left"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.term }}{{ termType(scope.row.termType) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('order.buyTime')"
            prop="createTime"
            align="left"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.createTime }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('order.payAmount')"
            prop="payAmount"
            align="left"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.payAmount }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('order.paymentMethod')"
            prop="stockBillType"
            align="left"
            :show-overflow-tooltip="true"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">
                <span v-if="scope.row.isPayed === 0">{{
                  $t("order.unpaid")
                }}</span>
                <span v-else>{{
                  [
                    $t("order.pointsPayment"),
                    $t("order.wecProPay"),
                    $t("order.alipayPCPayment"),
                    $t("order.wechatScanCodePayment"),
                    $t("order.wechatH5Payment"),
                    $t("order.weclAccountPay"),
                    $t("order.alipayH5Payment"),
                    $t("order.alipayAPPPayment"),
                    $t("order.wechatAPPPayment"),
                    $t("order.balancePayment"),
                    $t("order.payPalPayment")
                  ][scope.row.payType]
                }}</span>
              </span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>
  </div>
</template>

<script setup>
// 用于指向this

const termType = (val) => {
  switch (val) {
    case 1:
      return $t('coupon.day')
    case 2:
      return $t('user.week')
    case 3:
      return $t('user.month')
    case 4:
      return $t('user.season')
    case 5:
      return $t('user.year')
  }
}

let tempSearchForm = null // 保存上次点击查询的请求条件

const dataList = ref([])
const createDateRange = ref([]) // 购买时间范围
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = reactive({
  nickName: '',
  createStartTime: null, // 制单起始时间
  createEndTime: null, // 制单结束时间
  stockBillItemType: 1
}) // 搜索

onMounted(() => {
  getDataList(page)
})

const getDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/user/userLevelLog/pageBuyLevelLog'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.pageSize : pageParam.pageSize
        },
        tempSearchForm
      )
    )
  }).then(({ data }) => {
    // 国际化
    getVipInternationalization(data.records)
    dataList.value = data.records
    page.total = data.total
  }).catch(() => {

  })
}

const onSearch = (newData = false) => {
  getDataList(page, newData)
}
const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}

const searchFormRef = ref(null)
const resetForm = () => {
  searchFormRef.value.resetFields()
  searchForm.nickName = ''
  searchForm.createStartTime = null
  searchForm.createEndTime = null
  createDateRange.value = []
}
const createTimeChange = () => {
  if (!createDateRange.value || createDateRange.value.length === 0) {
    searchForm.createStartTime = null
    searchForm.createEndTime = null
  } else {
    searchForm.createStartTime = createDateRange.value[0]
    searchForm.createEndTime = createDateRange.value[1]
  }
}
// 国际化
const getVipInternationalization = (data) => {
  if (!data) return
  const lang = localStorage.getItem('bbcLang') || 'zh_CN'
  data.map(element => {
    let levelNameCn, levelNameEn
    if (!element.userLevelLangList) return element
    element.userLevelLangList.map(val => {
      if (val.lang === 0) {
        levelNameCn = val.levelName
      }
      if (val.lang === 1) {
        levelNameEn = val.levelName
      }
      return val
    })
    element.levelName = lang === 'zh_CN' ? levelNameCn : levelNameEn || levelNameCn
    element.levelName = lang === 'zh_CN' ? levelNameCn : levelNameEn || levelNameCn
    return element
  })
}

</script>

<style lang="scss" scoped>
div{
  :deep(.el-tabs__item.is-active){
    color:#000;
    font-weight: 800;
  }
  :deep(.el-col-md-6){
    width:30%
  }
  :deep(.table-cell-text){
    display: inline !important;
  }
}

</style>
